package com.yxq.mdt.app.modules.pm.ui;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsDetailBean;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.picturebrowse.ui.PictureActivity;
import com.yxq.mdt.app.modules.pm.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.pm.model.GoodsModel;
import com.yxq.mdt.app.modules.pm.presenter.GoodsPresenter;
import com.yxq.mdt.app.modules.pm.presenter.IGoodsContract;
import com.yxq.mdt.app.utils.DisplayUtil;
import com.yxq.mdt.app.widgets.tagview.TagView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 商品详情-首页
 */
public class GoodsDetailIndexFragment extends BaseFragment<GoodsPresenter> implements IGoodsContract.View {

    @BindView(R.id.goods_img_banner)
    ConvenientBanner goodsImgBanner;
    @BindView(R.id.goods_tv_price)
    TextView goodsTvPrice;
    @BindView(R.id.goods_tv_sale_count)
    TextView goodsTvSaleCount;
    @BindView(R.id.goods_detail_tags)
    TagView goodsDetailTags;
    @BindView(R.id.goods_detail_tv_goods_name)
    TextView goodsDetailTvGoodsName;
    @BindView(R.id.goods_detail_tv_goods_label)
    TextView goodsDetailTvGoodsLabel;
    GoodsDetailBean mGoodsDetailBean;
    @BindView(R.id.goods_detail_tv_service)
    TextView goodsDetailTvService;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_goods_detail_index_gift_package;
    }

    @Override
    protected GoodsPresenter getPresenter() {
        return new GoodsPresenter(this, new GoodsModel());
    }

    @Override
    public void initView(View view) {
        int screenWidth = DisplayUtil.getScreenRelatedInformation(getContext());
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) goodsImgBanner.getLayoutParams();
        layoutParams.height = screenWidth;
        goodsImgBanner.setLayoutParams(layoutParams);
        goodsImgBanner.setPageIndicator(new int[]{R.drawable.shape_banner_selector_unchecked, R.drawable.shape_banner_selector_checked});
    }

    @Override
    public void initData() {
        mPresenter.getGoods(getArguments().getString("id"));
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onLoadSuccess(GoodsDetailBean goodsDetailBean) {
        if (goodsDetailBean == null) {
            return;
        }
        mGoodsDetailBean = goodsDetailBean;
        initGoodsImages(goodsDetailBean);
        initGoodsInfo(goodsDetailBean);
    }

    /**
     * 初始化商品文字介绍信息
     *
     * @param goodsDetailBean
     */
    private void initGoodsInfo(GoodsDetailBean goodsDetailBean) {
        goodsDetailTvGoodsName.setText(goodsDetailBean.getName());
//        if (goodsDetailBean.getMin_price() == goodsDetailBean.getMax_price()) {
//            goodsTvPrice.setText(String.format("￥%.2f", goodsDetailBean.getMin_price()));
//        } else {
//            goodsTvPrice.setText(String.format("￥%.2f~￥%.2f", goodsDetailBean.getMin_price(), goodsDetailBean.getMax_price()));
//        }
        goodsTvPrice.setText(String.format("￥%.2f", goodsDetailBean.getPrice()));
        goodsTvSaleCount.setText(String.format("销量:%s", goodsDetailBean.getSales_volume()));
        if(goodsDetailBean.getService_list()!=null){
            goodsDetailTvGoodsLabel.setText(goodsDetailBean.getService_list().size() > 0 ? goodsDetailBean.getService_list().toString() : "");
        }
    }

    /**
     * 初始化商品滚动图片
     *
     * @param goodsDetailBean
     */
    private void initGoodsImages(GoodsDetailBean goodsDetailBean) {
        if(goodsDetailBean==null){
            return;
        }
        if(goodsDetailBean.getPic_list()==null){
            return;
        }
        goodsImgBanner.setPages(new CBViewHolderCreator() {
            @Override
            public Holder createHolder(View itemView) {

                return new ImageHolderView(itemView);
            }

            @Override
            public int getLayoutId() {
                return R.layout.layout_banner_item;
            }
        }, goodsDetailBean.getPic_list());
        goodsImgBanner.startTurning();
        goodsImgBanner.setOnItemClickListener(position -> {
            //预览
            Intent intent = new Intent(getContext(), PictureActivity.class);
            intent.putExtra("position", position);
            ArrayList<String> images = new ArrayList<>();
            for (GoodsDetailBean.PicListBean photoImgFile : goodsDetailBean.getPic_list()) {
                images.add(photoImgFile.getPic_url());
            }
            intent.putExtra("images", images);
            startActivity(intent);
        });
    }

    @Override
    public void onLoadFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onLoadRecommendSuccess(MallGoodsBean mallGoodsBean) {

    }

    @Override
    public void onLoadRecommendFailed(String err) {

    }

    @Override
    public void onAddSuccess(String err) {
        ToastUtils.showShort(err);
        if (getGoodsDetailBean() != null) {
            getGoodsDetailBean().setIs_favorite(1);
            collectStatus.setImageResource(R.mipmap.ic_collect);
        }
    }

    @Override
    public void onAddFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onRemoveSuccess(String err) {
        ToastUtils.showShort(err);
        if (getGoodsDetailBean() != null) {
            getGoodsDetailBean().setIs_favorite(0);
            collectStatus.setImageResource(R.mipmap.ic_collect);
        }
    }

    @Override
    public void onRemoveFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onLoadCommentSuccess(GoodsCommentBean goodsCommentBean) {

    }

    @Override
    public void onLoadCommentFailed(String err) {

    }

    @Override
    public void onAddCartSuccess(String result) {

    }

    @Override
    public void onAddCartFailed(String err) {

    }

    ImageView collectStatus;

    public void add(ImageView collectStatus) {
        this.collectStatus = collectStatus;
        if (getGoodsDetailBean() != null) {
            if (getGoodsDetailBean().getIs_favorite() == 1) {
                mPresenter.favoriteRemove(getArguments().getString("id"));
            } else {
                mPresenter.favoriteAdd(getArguments().getString("id"));
            }
        }

    }

    /**
     * 返回商品详情
     *
     * @return
     */
    public GoodsDetailBean getGoodsDetailBean() {
        return mGoodsDetailBean;
    }

    /**
     * 图片轮转实现
     */
    class ImageHolderView extends Holder<GoodsDetailBean.PicListBean> {
        private ImageView imageView;

        public ImageHolderView(View itemView) {
            super(itemView);
        }

        @Override
        protected void initView(View itemView) {
            imageView = itemView.findViewById(R.id.banner_image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }

        @Override
        public void updateUI(GoodsDetailBean.PicListBean bean) {
            GlideHelper.setLoadImage(mContext, bean.getPic_url(),  R.color.gray, imageView);
        }
    }
}